import axios from 'axios'
import { ElNotification } from 'element-plus'

const request = axios.create({
    baseURL: 'http://121.89.205.189:3000/admin'
})

// 请求拦截器
request.interceptors.request.use((config) => {
    // console.log(config);
    // 获取token 
    const token = localStorage.getItem('token')
    // console.log(token);
    config.headers.token = token
    return config
})


// 响应拦截器
request.interceptors.response.use((res) => {
    // console.log(res);
    const { method,silence } = res.config
    const { code, message } = res.data
    // console.log(code);
    if (code === '200') {
        if (!silence && (method !== 'get')) {
            ElNotification({
                title: '成功',
                message: message,
                type: 'success',
            })
            return res.data.data
        }
    } else {
        ElNotification({
            title: '失败',
            message: message,
            type: 'error',
        })
    }
    return res.data
}, (error) => {
    console.log(error);
})


export default request